package com.c.ctgproject.adapter.guang

import android.content.Context
import android.text.Editable
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import androidx.appcompat.widget.LinearLayoutCompat
import androidx.recyclerview.widget.RecyclerView
import com.c.ctgproject.R
import com.c.ctgproject.base.BaseEmptyRecyclerViewAdapterV2
import com.c.ctgproject.entity.guang.MaterialOutSubmitBean
import com.c.ctgproject.utils.MyLog
import com.c.ctgproject.utils.handlerNull

class MaterialCheckInputAdapter(private val context: Context, private val mList: List<MaterialOutSubmitBean>, private val listener: TestListener) : BaseEmptyRecyclerViewAdapterV2(context, mList) {
    override fun createViewHolder(parent: ViewGroup): RecyclerView.ViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.item_material_check_input_list, parent, false)
        return MyViewHolder(view)
    }

    override fun handlerData(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is MyViewHolder) {
            val data = mList[position]
            holder.mRemoveBTN.visibility=if (mList.size==1) View.GONE else View.VISIBLE
            holder.number.text = data.count.handlerNull()
            //holder.number.setText(data.count.handlerNull())
//            holder.countUnit.text = data.unit.handlerNull()
            val material = if (data.materialName.handlerNull().isEmpty()) "请选择物料" else data.materialName.handlerNull()
            holder.mMaterialTV.text = material
            holder.mRemoveBTN.setOnClickListener {
                MyLog.d("你点的第几条:$position")
                listener.onRemoveClick(position)
            }

            //选择物料
            holder.mMaterialLL.setOnClickListener {
                listener.onSelectMaterialSelect(position)
            }
            val watcher = object : TextWatcher {
                override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

                }

                override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

                }

                override fun afterTextChanged(s: Editable?) {
                    when {
                        holder.number.hasFocus() -> {
                            listener.onNumberChange(s.toString().handlerNull(), position)
                        }
                    }
                }
            }

            holder.number.setOnFocusChangeListener { _, hasFocus ->
                if (hasFocus) {
                    holder.number.addTextChangedListener(watcher)
                } else {
                    holder.number.removeTextChangedListener(watcher)
                }
            }
//            holder.countUnit.setOnFocusChangeListener { _, hasFocus ->
//                if (hasFocus) {
//                    holder.countUnit.addTextChangedListener(watcher)
//
//                } else {
//                    holder.countUnit.removeTextChangedListener(watcher)
//                }
//            }
        }
    }

    inner class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val number: TextView = view.findViewById(R.id.number)
        //val number: EditText = view.findViewById(R.id.number)
//        val countUnit: TextView = view.findViewById(R.id.countUnit)
        val mMaterialTV: TextView = view.findViewById(R.id.mMaterialTV)
        val mMaterialLL: LinearLayoutCompat = view.findViewById(R.id.mMaterialLL)
        val mRemoveBTN: Button = view.findViewById(R.id.mRemoveBTN)

    }

    interface TestListener {
        fun onRemoveClick(position: Int)
        fun onSelectMaterialSelect(position: Int)
        fun onNumberChange(number: String, position: Int)
        fun onUnitChange(unit: String, position: Int)

    }
}